package com.zp.demopractice1.me;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zp.demopractice1.MainActivity;
import com.zp.demopractice1.Utils.UIUtils;

import java.util.List;

/**
 * Created by izp on 2017/6/14.
 */

public class PagerIndicatorView extends LinearLayout {
    private int mTotalSize = 4;
    private int mCurrentPosition;
    private Pair<Drawable, Drawable> drawables;
    private ImageView[] points;
    private ViewPager mViewPager;
    public PagerIndicatorView(Context context) {
        super(context);
        KLog.v("这里运行了么");
        init();
    }

    public PagerIndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        KLog.v("这里运行了么");
        init();
    }

    public PagerIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        KLog.v("这里运行了么");
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public PagerIndicatorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        KLog.v("这里运行了么");
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        drawables = new Pair<>(getOvalDrawable(false),getOvalDrawable(true));

    }
    public void bindWithViewPager(ViewPager pager,int mTotalSize){
        this.mViewPager = pager;
        points = new ImageView[mTotalSize];
        for(int i=0;i<mTotalSize;i++){
            KLog.v("这里运行了么");
            ImageView imageView = new ImageView(getContext());
            imageView.setImageDrawable(drawables.first);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            LayoutParams layoutParams = new LayoutParams(UIUtils.dpToPx(16),UIUtils.dpToPx(16));
            imageView.setLayoutParams(layoutParams);
            points[i] = imageView;
            addView(imageView);
        }
        updatePosition(0);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updatePosition(position);
                KLog.v("当前子View:"+mViewPager.getChildCount());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updatePosition(int currentPosition){
        for (ImageView imageView:points){
            imageView.setImageDrawable(drawables.first);
        }
        points[currentPosition].setImageDrawable(drawables.second);
    }
    private Drawable getOvalDrawable(boolean active){
        ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
        shapeDrawable.getPaint().setColor(Color.WHITE);
        if (!active){
            shapeDrawable.setAlpha(150);
        }
        shapeDrawable.setIntrinsicHeight(UIUtils.dpToPx(6));
        shapeDrawable.setIntrinsicWidth(UIUtils.dpToPx(6));
        return shapeDrawable;
    }

}
